package cn.android.support.v7.lib.sin.crown.dialog;

import android.app.Activity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

import java.util.ArrayList;
import java.util.List;

import cn.android.support.v7.lib.sin.crown.R;
import cn.android.support.v7.lib.sin.crown.base.BaseApplication;
import cn.android.support.v7.lib.sin.crown.base.BaseDialog;
import cn.android.support.v7.lib.sin.crown.base.BaseListenner;
import cn.android.support.v7.lib.sin.crown.utils.ProportionUtils;
import cn.android.support.v7.lib.sin.crown.view.WheelTurns;

/**
 * 自定义滚轮数据
 * Created by 彭治铭 on 2018/1/7.
 */

public class RollerDialog extends BaseDialog {

    private static RollerDialog rollerDialog;

//                调用案例
//                List<String> infos = new ArrayList<>();
//                final String[] selectValue = new String[1];//直接  String selectValue 即可。
//
//                infos.add("男");
//                infos.add("女");
//                infos.add("男女不限");
//                selectValue[0] = infos.get(1);
//
//                Log.e("test", "当前值:\t" + selectValue[0]);
//                RollerDialog.getInstance(MainActivity.this).setData(infos, selectValue[0], new BaseListenner<String>() {
//                    @Override
//                    public void onResult(String result) {
//                        selectValue[0] = result;
//                    }
//                }).setBtnInfoText("请选择性别");

    public static RollerDialog getInstance(Activity activity) {
        if (isfinish(activity,rollerDialog)) {
            rollerDialog = new RollerDialog(activity);
        }
        return rollerDialog;
    }

    private RollerDialog(Activity activity) {
        init(activity, R.layout.crown_dialog_roller);
        window.setWindowAnimations(R.style.crown_window_bottom);
        initEx();
    }


    Button buttonComplete;
    WheelTurns roller;


    public Button crown_btn_cancel, crown_btn_info;

    //设置右边完成按钮信息
    public RollerDialog setBtnCompleteText(String info) {
        buttonComplete.setText(info);
        return this;
    }

    //设置右边完成按钮字体颜色
    public RollerDialog setBtnCompleteColor(int color) {
        buttonComplete.setTextColor(color);
        return this;
    }

    //设置中间按钮信息
    public RollerDialog setBtnInfoText(String info) {
        crown_btn_info.setText(info);
        return this;
    }

    //设置中间按钮字体颜色
    public RollerDialog setBtnInfoColor(int color) {
        crown_btn_info.setTextColor(color);
        return this;
    }


    //设置取消按钮信息
    public RollerDialog setBtnCancelText(String info) {
        crown_btn_cancel.setText(info);
        if (info == null || info.trim().equals("")) {
            crown_btn_cancel.setOnClickListener(null);
        } else {
            crown_btn_cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
        }
        return this;
    }

    //设置取消按钮字体颜色
    public RollerDialog setBtnCancelColor(int color) {
        crown_btn_cancel.setTextColor(color);
        return this;
    }


    private void initEx() {
        crown_btn_info = (Button) findViewById(R.id.crown_btn_info);
        crown_btn_cancel = (Button) findViewById(R.id.crown_btn_cancel);
        ProportionUtils.getInstance().adapterButton(crown_btn_cancel);
        ProportionUtils.getInstance().adapterButton(crown_btn_info);
        crown_btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }


    @Override
    protected void initUI() {
        buttonComplete = (Button) findViewById(R.id.crown_btn_complete);
        roller = (WheelTurns) findViewById(R.id.roller);

        width = (int) ProportionUtils.getInstance().realWidthPixels;
        height = ProportionUtils.getInstance().adapterInt(360);
        count = 5;
    }

    @Override
    protected void adapterUI() {
        ProportionUtils.getInstance().adapterScreen(activity, findViewById(R.id.crown_roller_dialog));
        ProportionUtils.getInstance().adapterView(findViewById(R.id.crown_startLine));
        ProportionUtils.getInstance().adapterView(findViewById(R.id.crown_endLine));
        ProportionUtils.getInstance().adapterButton(buttonComplete);
    }

    @Override
    protected boolean isAutoAdapterUI() {
        return false;
    }

    @Override
    protected boolean isStatus() {
        return true;
    }

    @Override
    protected boolean isTransparent() {
        return false;
    }

    @Override
    protected boolean isLocked() {
        return false;
    }

    @Override
    protected void listener() {

        findViewById(R.id.crown_roller_dialog_bg).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    dismiss();
                }
                return true;
            }
        });

        buttonComplete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (baseListenner != null) {
                    List<Integer> list = new ArrayList<Integer>();
                    baseListenner.onResult(roller.curentValue);
                }
                dismiss();
            }
        });

        roller.postDelayed(new Runnable() {
            @Override
            public void run() {
                //选中默认值
                roller.selectVaules(selectValue, false);
            }
        }, 200);//测试发现，至少都需要125毫秒，才会有选中刷新效果。200毫秒是确保能够刷新。【200毫秒，肉眼是看不出来的】
    }

    @Override
    protected void recycleView() {

    }

    int width, height, count;


    List<String> infos;//数据
    String selectValue;//当前选中值
    BaseListenner<String> baseListenner;

    /**
     * @param infos         数据数组
     * @param selectValue   但前选中值，是数组里面的值。
     * @param baseListenner 回调函数，返回当前选中值。
     * @return
     */
    public RollerDialog setData(List<String> infos, final String selectValue, BaseListenner<String> baseListenner) {
        if (this.infos != infos || this.infos == null) {
            this.infos = infos;
            this.baseListenner = baseListenner;
            roller.setData(infos, "", width, height, count);
        }
        this.selectValue = selectValue;
        return this;
    }

    public static void setRollerDialog(RollerDialog rollerDialog) {
        RollerDialog.rollerDialog = rollerDialog;
    }

    public void setButtonComplete(Button buttonComplete) {
        this.buttonComplete = buttonComplete;
    }

    public void setRoller(WheelTurns roller) {
        this.roller = roller;
    }

    public void setCrown_btn_cancel(Button crown_btn_cancel) {
        this.crown_btn_cancel = crown_btn_cancel;
    }

    public void setCrown_btn_info(Button crown_btn_info) {
        this.crown_btn_info = crown_btn_info;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public void setInfos(List<String> infos) {
        this.infos = infos;
    }

    public void setSelectValue(String selectValue) {
        this.selectValue = selectValue;
    }

    public void setBaseListenner(BaseListenner<String> baseListenner) {
        this.baseListenner = baseListenner;
    }
}
